package controller

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	v1 "seawolf-admin-go/api/admin/v1"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/routers"
	"seawolf-admin-go/internal/service/AdminService"
	"seawolf-admin-go/utility/myCaptcha"
	token2 "seawolf-admin-go/utility/token"
)

type cOpen struct {
	*BaseController
}

func NewOpenController(group *ghttp.RouterGroup, defaultVersion string) {
	// 初始化内部变量
	inputReq := &BaseReq.I{}
	c := &cOpen{&BaseController{Req: inputReq}}
	c.New(group, defaultVersion)
}

func (c *cOpen) New(group *ghttp.RouterGroup, defaultVersion string) {
	group.POST("/login", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.Login,
		}, defaultVersion, routers.DemoteStrategy)
	})

	group.GET("/test", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.Test,
		}, defaultVersion, routers.DemoteStrategy)
	})

	group.GET("/captcha", func(r *ghttp.Request) {
		routers.MethodCallWithVersionStrategy(r, map[string]interface{}{
			"1.0.0": c.Captcha,
		}, defaultVersion, routers.DemoteStrategy)
	})
}

func (c *cOpen) Login(r *ghttp.Request, req *v1.LoginReq) (res *v1.LoginRes, err error) {
	res, err = AdminService.Login(r.Context(), req)
	if err != nil {
		return nil, err
	}
	return
}

func (c *cOpen) Test(r *ghttp.Request) (res *v1.LoginRes, err error) {
	token2.UpdateSessionData(r.Context(), 22, g.Map{
		"test": "test",
	})
	return
}

func (c *cOpen) Captcha(r *ghttp.Request) error {
	r.Response.ClearBuffer()
	r.Response.WriteJsonExit(myCaptcha.CreateCaptcha(110, 36))
	return nil
}
